//
//  JsLoginViewController.m
//  JsUtilitySdk
//
//  Created by Gikki Ares on 2023/4/8.
//

#import "JsLoginViewController.h"
#import "JsTextField.h"
#import "JsTextFieldVm.h"
#import "JsBundleManager.h"
@interface JsLoginViewController ()<
	JsTextFieldDelegate
>{
	
	__weak IBOutlet JsTextField *mTf_password;
	__weak IBOutlet JsTextField *mTf_account;
	__weak IBOutlet UIButton *mBtn_login;
}

@end

@implementation JsLoginViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if(!nibNameOrNil) {
		nibNameOrNil = NSStringFromClass([self class]);
		nibBundleOrNil = [JsBundleManager jsUtilityBundle];
	}
	if(self == [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		
	}
	return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	self.title = @"登录";
	{
		JsTextFieldVm * vm = [JsTextFieldVm defaultVm];
		vm.textColor = [UIColor blackColor];
		vm.placeholder = @"账号";
		vm.invalidReminder = @"账号长度大于等于3位";
		mTf_account.vm = vm;
		vm.text = self.account;
		mTf_account.delegate = self;
	}
	{
		JsTextFieldVm * vm = [JsTextFieldVm defaultVm];
		vm.textColor = [UIColor blackColor];
		vm.placeholder = @"密码";
		vm.text = self.password;
		vm.isHideContent = YES;
		vm.invalidReminder = @"密码长度大于等于4位";
		mTf_password.vm = vm;
		mTf_password.delegate = self;
	}
}


#pragma mark 4 Delegate
- (BOOL)jsTextField:(JsTextField *)tf isContentValid:(NSString *)content {
	if(tf == mTf_account) {
		if(content.length>=3){
			return YES;
		}
		else {
			return NO;
		}
	}
	else if(tf == mTf_password){
		if(content.length>=4) {
			return YES;
		}
		else {
			return NO;
		}
	}
	return NO;
}


#pragma mark 5 Event
#pragma mark 5.1 BtnClickEvent
- (IBAction)onLoginBtnClick:(id)sender {
	BOOL isContinue = YES;
	
	if(!mTf_account.isContentValid) {
		[mTf_account showReminder:YES shake:YES];
		isContinue = NO;
	}
	if(!mTf_password.isContentValid) {
		[mTf_password showReminder:YES shake:YES];
		isContinue = NO;
	}
	if(isContinue) {
		if(self.delegate && [self.delegate respondsToSelector:@selector(jsLoginViewControllerWillLogin:)]) {
			 [self.delegate jsLoginViewControllerWillLogin:self];
		}
	}
}
#pragma mark 5.1 NotificationEvent


@end
